package ace.states;

import java.io.IOException;
import java.util.Vector;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;

import ace.component.Button;
import ace.component.ButtonPush;
import ace.system.StateManager;
import ace.system.Utilities;
import ace.system.sound.SFXPlayer;
import ace.system.sound.SimplePlayer;

public class StateFront extends GameState {
	public StateFront() {
		super(GameState.FRONT, StateManager.instance().getBackgroundImage());
		
		//Initialize
		m_Draw		= true;
		Animate		= true;
		m_Selected 	= 0;
		
		//Create buttons
		createButtons();
		
		//Create images
		try {
			m_TitleImage	= Image.createImage(Utilities.INTERFACE_FOLDER + "/title.png");
			m_LiteImage		= Image.createImage(Utilities.INTERFACE_FOLDER + "/lite.png");
			m_VersionImage	= Image.createImage(Utilities.INTERFACE_FOLDER + "/version.png");
			//m_ReturnImage	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText(StringManager.instance().getString("exit"), 0x00000000);
			//m_TitleArrowImage = Image.createImage(Utilities.INTERFACE_FOLDER + "/title_arrow.png");
		} catch (IOException ex) {}

		//Initialize animation
		m_AnimationTime	= 0;
		m_ButtonsY		= StateManager.instance().getHeight();
		m_Entering		= false;
		m_Exiting		= false;

		StateManager.instance().setSoundOff(true);
	}
	
	protected void createButtons() {
		m_Menu = new Vector();
		for(int i = 0; i < m_MenuStrings.length; ++i){
			ButtonPush bp = new ButtonPush(m_MenuStrings[i], i, StateManager.instance().getWidth()-5, 80+i*20);
			bp.setAlignment(Graphics.RIGHT | Graphics.VCENTER);
			bp.setPointerAlignment(Graphics.LEFT | Graphics.VCENTER);
			bp.setPointerPosition(StateManager.instance().getWidth() - 98, 0);
			
			m_Menu.addElement(bp);
		}

		((ButtonPush)m_Menu.elementAt(m_Selected)).setState(Button.STATE_SELECTED);
	}

	public void onEnter(){
		StateManager.instance().getSimplePlayer().setSong(Utilities.BGM_FOLDER + "/title.mid", "audio/mid", SimplePlayer.LOOP_INF);
		StateManager.instance().getSimplePlayer().startPlayer();

		//Draws
		m_Draw = true; 
		
		//Animate?
		if (Animate) m_Entering = true;
	}

	public void keyPressed(int key) {
		//Super
		super.keyPressed(key);

		//Is right pressed?
		if (key == -7 || key == 35) {
			StateManager.instance().getSFXPlayer().playSFX(SFXPlayer.SFX_CANCEL);
			
			//Exit
			m_Active = false;
		}
	}
	
	public void update(long time, int keys) {
		//Super
		super.update(time, keys);
		
		//If entering
		if (m_Entering) {
			//Skip if first time
			if (m_AnimationTime == 0) m_AnimationTime = 1;
			else {			
				//Increase
				m_AnimationTime += time;
				m_Draw			= true;

				//Set
				float Comparison	= (float)m_AnimationTime / (float)ANIMATE_TIME;
				m_ButtonsY			= StateManager.instance().getHeight() - (int)((StateManager.instance().getHeight() - 83) * Comparison);

				//Check time and fire
				if (m_AnimationTime > ANIMATE_TIME || keys != 0) {
					m_Entering		= false;
					m_AnimationTime = 0;

					//Finalize button
					m_ButtonsY	= 83;
				}
			}
		} else if (m_Exiting) {
			if (m_AnimationTime < 0) {
				//No longer exiting
				m_Exiting		= false;
				m_AnimationTime = 0;
				
				//Parameters
				Vector param = new Vector();
				
				//Based on ID
				switch (m_Selected) {
				case 0:
					//Play, Go to game state
					StateManager.instance().goTo(GameState.PROFILE, null, false);
					break;

				case 2:
					//Help
                    Vector parameter = new Vector();
                    parameter.addElement(new Integer(GameState.HELP));
					StateManager.instance().goTo(GameState.LOADING, parameter, false);
					break;

				case 3:
					//Credit
					StateManager.instance().goTo(GameState.CREDITS, null, false);
					break;

				case 4:
					//Exit, Deactivate state
					m_Active = false;
					break;
				}
			} else {
				//Increase
				m_Draw = true;
				m_AnimationTime += time;

				//Set
				float Comparison	= (float)m_AnimationTime / (float)ANIMATE_TIME;
				m_ButtonsY			= 85 + (int)((StateManager.instance().getHeight() - 85) * Comparison);

				//Check time
				if (m_AnimationTime > ANIMATE_TIME || keys != 0) m_AnimationTime = -1;
			}
		} else {
			//Check key
			if ((keys & GameCanvas.UP_PRESSED) != 0 || (keys & GameCanvas.DOWN_PRESSED) != 0) {
				StateManager.instance().getSFXPlayer().playSFX(SFXPlayer.SFX_CURSOR);

				if((keys & GameCanvas.UP_PRESSED) != 0){
					m_Selected--;
					if(m_Selected < 0)	m_Selected = m_MenuStrings.length-1;
				} else { //Down pressed
					m_Selected++;
					if(m_Selected >= m_MenuStrings.length) m_Selected = 0;
				}

				for(int i = 0; i < m_MenuStrings.length; ++i){
					((ButtonPush)m_Menu.elementAt(i)).setState(Button.STATE_NONE);
				}
				((ButtonPush)m_Menu.elementAt(m_Selected)).setState(Button.STATE_SELECTED);

				//Draw
				m_Draw = true;
			}

			//If button is pressed
			if ((keys & GameCanvas.FIRE_PRESSED) != 0) {
				//Toggle on/off
				if (m_Selected == 1) {
					//Toggle sound
					boolean Mute = StateManager.instance().isSoundOff();
					StateManager.instance().setSoundOff(!Mute);

					String onOff = StateManager.instance().isSoundOff() == true ? "off" : "on";
					((ButtonPush)m_Menu.elementAt(m_Selected)).setText("Sound: " + onOff);
				} else {
					m_Exiting = true;
				}
				((ButtonPush)m_Menu.elementAt(m_Selected)).setState(Button.STATE_PRESSED);
				
				//Draw
				m_Draw = true;
			}
		}
	}
	
	public void draw(Graphics g) {
		//Skip if not drawn
		if (!m_Draw) return;
		
		//Super
		super.draw(g);
		
		//Draw stuff
		for(int i = 0; i < m_MenuStrings.length; ++i){
			((ButtonPush)m_Menu.elementAt(i)).setY(m_ButtonsY + i*20);
			((ButtonPush)m_Menu.elementAt(i)).draw(g);
		}
		
		//If not animating
		if (!m_Entering && !m_Exiting) {
			//g.drawImage(m_TitleArrowImage, -5, -15, Graphics.LEFT | Graphics.TOP);
			g.drawImage(m_TitleImage, 5, 98, Graphics.LEFT | Graphics.TOP);
			g.drawImage(m_LiteImage, 160, 130, Graphics.LEFT | Graphics.TOP);
			g.drawImage(m_VersionImage, StateManager.instance().getWidth(), 0, Graphics.RIGHT | Graphics.TOP);
			//g.drawImage(m_ReturnImage, StateManager.instance().getWidth() - 8, StateManager.instance().getHeight()-4, Graphics.RIGHT | Graphics.BOTTOM);
		}
			
		//Don't draw
		m_Draw = false;
	}

	protected final long ANIMATE_TIME = 500;
	
	//Animate toggle
	public static boolean Animate;

	//Data
	protected int			m_Selected;
	protected boolean		m_Draw;
	protected Vector		m_Menu;
	protected String[]		m_MenuStrings = {"Play", "Sound: off", "Tutorial", "Credit", "Exit"};
	
	//Animation
	protected int		m_ButtonsY;
	protected boolean	m_Entering;
	protected boolean	m_Exiting;
	protected int		m_AnimationTime;
	
	//Images
	protected Image m_LiteImage;
	protected Image	m_TitleImage;
	//protected Image	m_ReturnImage;
	protected Image m_VersionImage;
}
